/*  
 *  
 * Copyright (c) 2007 John Wells 
 * Copyright (c) 2010 Photonic Games 
 
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following conditions: 
 
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameLevel.ContentPipeline.Leadwerks14
{
    public sealed class Terrain
    {
        public class Layer
        {
            public String layerName;
            public Material material;
            public int materialIndex;
            public byte[] alpha;
        }

        public Byte flags;
        public Vector3 position;
        public float width;
        public float height;
        public string terrainName;
        public Int32 resolution;
        public Int32 sectors;
        public Int32 detailLevels;
        public float lightmapResolution;
        public Int32 layerCount;

        public Layer[] layers;
        public float[] heightMap;
        public Byte[] lightMap;

        public Terrain(BinaryReader reader, string[] names)
        {
            
            flags = reader.ReadByte();
            position = Leadwerks14Importer.ReadVector3(reader);
            width = reader.ReadSingle();
            height = reader.ReadSingle();
            //terrainName = reader.ReadString();  
            terrainName = names[reader.ReadInt32() - 1];
            resolution = reader.ReadInt32();
            sectors = reader.ReadInt32();
            detailLevels = reader.ReadInt32();
            lightmapResolution = reader.ReadSingle();
            layerCount = reader.ReadInt32();


            if ((flags & 1) != 0)
            {
                lightMap = new Byte[(resolution) * (resolution) * 4];
                for (int index = 0; index < (resolution) * (resolution) * 4; index+= 4)
                {
                    lightMap[index] = reader.ReadByte();
                    lightMap[index + 1] = reader.ReadByte();
                    lightMap[index + 2] = reader.ReadByte();
                    lightMap[index + 3] = 0xFF;
                }
            }

            heightMap = new float[(resolution + 1) * (resolution + 1)];

            for (int index = 0; index < (resolution + 1) * (resolution + 1); index++)
            {
                heightMap[index] = reader.ReadSingle();
            }

            layers = new Layer[layerCount];

            for (Int32 index = 0; index < layerCount; index++)
            {
                layers[index] = new Layer();
                layers[index].layerName = names[reader.ReadInt32()-1];
                layers[index].materialIndex = reader.ReadInt32()-1;

                layers[index].alpha = new Byte[resolution * resolution];

                if (index > 0)
                {
                    for (int index2 = 0; index2 < (resolution) * (resolution); index2++)
                    {
                        layers[index].alpha[index2] = reader.ReadByte();
                    }
                }
            }
        }
    }
}

